package game_logic.view;

import game_logic.control.listener.Listener;
import game_logic.model.Board;
import game_logic.util.Utils;

import javax.swing.*;

/**
 * 实现棋盘视图接口
 */
public class SimpleBoardView implements BoardView {
    public static final int VIEW_WIDTH = 700, VIEW_HEIGHT = 712; // 屏幕宽高
    public static final int PIECE_WIDTH = 67, PIECE_HEIGHT = 67; // 棋子宽高
    public static final int UNIT_SIZE = 68; // 视图坐标与位置坐标的倍数关系
    public static final int START_OFFSET_X = 50, START_OFFSET_Y = 15; // 从位置坐标映射到视图坐标的初始偏移量
    private final JFrame frame; // 主窗口
    private final JLayeredPane pieces; // 棋盘、棋子、出手方的 Pane
    private final PieceView pieceView; // 更新棋子 Pane
    private final Listener listener; // 监听器
    private final boolean reverseBoard; // 棋盘是否反转

    /**
     * 构造函数，传入游戏监听器
     *
     * @param listener 游戏监听器
     */
    public SimpleBoardView(Listener listener, boolean reverseBoard) {
        this.reverseBoard = reverseBoard;
        // init
        frame = new JFrame("Intelligent Chinese Chess");
        frame.setIconImage(new ImageIcon("res/img/icon.png").getImage());
        frame.setSize(VIEW_WIDTH, VIEW_HEIGHT + 40);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        // 注册监听器
        this.listener = listener;
        frame.addMouseListener(listener);
        // 初始化棋子、出手方
        pieces = new JLayeredPane();
        frame.add(pieces);
        pieceView = new SimplePieceView(pieces, reverseBoard);
        frame.setVisible(true);
    }


    @Override
    public void display(Board board) {
        pieces.removeAll();
        // 初始化棋盘
        JLabel bgBoard = new JLabel(new ImageIcon("res/img/board.png"));
        bgBoard.setLocation(0, 0);
        bgBoard.setSize(VIEW_WIDTH, VIEW_HEIGHT);
        pieces.add(bgBoard, JLayeredPane.DEFAULT_LAYER); // 放在最底层
        // 出手方
        pieces.add(this.makeJLabelTurn(board.getCurrentPlayer()), JLayeredPane.MODAL_LAYER);  // 放在顶层
        // 棋子视图
        pieceView.display(board);
        // 显示点击位置
        int[] modelPos = this.listener.getFirstPos();
        if (modelPos != null) pieces.add(this.makeClick(modelPos), JLayeredPane.MODAL_LAYER);  // 放在顶层
    }

    /**
     * 根据传入的信息，构造显示棋子的 JLabel
     *
     * @param CurrentPlayer 棋子
     * @return 显示棋子的 JLabel
     */
    private JLabel makeJLabelTurn(int CurrentPlayer) {
        String turn = String.format("res/img/%s.png", CurrentPlayer == 0 ? "redTurn" : "blackTurn");
        JLabel retLabel = new JLabel(new ImageIcon(turn));
        retLabel.setLocation(10, 320);
        retLabel.setSize(PIECE_WIDTH, PIECE_HEIGHT);
        return retLabel;
    }

    /**
     * 根据指定坐标信息，构造显示点击效果的 JLabel
     *
     * @param modelPos 指定坐标信息
     * @return 显示点击效果的 JLabel
     */
    private JLabel makeClick(int[] modelPos) {
        int[] viewPos = Utils.getViewPos(modelPos, reverseBoard);
        JLabel retLabel = new JLabel(new ImageIcon("res/img/click.png"));
        retLabel.setLocation(viewPos[0], viewPos[1]);
        retLabel.setSize(PIECE_WIDTH, PIECE_HEIGHT);
        return retLabel;
    }


}
